#include "configer.h"
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include"dl_lua.h"
#include"app_struct.h"
#include"app_global.h"

static char s_lua_filepath[1024] = { 0 };
static void enum_log_config(lua_State* L)
{
	lua_getglobal(L, "Log");
	if (!lua_istable(L, -1))
	{
		printf("-Log-error!%s--%s--%d\n", __FILE__, __FUNCTION__, __LINE__);
		return;
	}
	dl_log_t log_cfg = { 0 };
	getfield_string(L, "Dir", log_cfg.dir);
	log_cfg.level = getfield_number(L, "Level");
	log_cfg.style = getfield_number(L, "Style");
	log_cfg.maxfiles = (int)getfield_number(L, "MaxCnt");

	size_t size = sizeof(log_cfg.prefix);
	lua_pop(L, 1);
}

static void enum_smartX_svr_config(lua_State* L)
{
	lua_getglobal(L, "CoreServer");
	if (!lua_istable(L, -1))
	{
		printf("-%s-error!%s--%s--%d\n", "CoreServer", __FILE__, __FUNCTION__, __LINE__);
		lua_pop(L, 1);
		return;
	}
	getfield_string(L, "IP", g_server_cfg.ip);
	g_server_cfg.port = (unsigned short)getfield_number(L, "Port");
	lua_pop(L, 1);
}

bool init_dl_cfg(const char* filename)
{
	lua_State* L = luaL_newstate();
	luaL_openlibs(L);
	int k = luaL_loadfile(L, filename);
	if (k != 0)
	{
		printf("can't open file:\"%s\" \n", filename);
		return false;
	}
	int ret = lua_pcall(L, 0, 0, 0);

	enum_log_config(L);
	enum_smartX_svr_config(L);
	lua_close(L);
	return true;
}